<?php
/**
 * Geneone: Content Management Framework
 * Redirect Object View
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
require_once ("Gene/Object/View.php");

class Gene_Object_View_Redirect extends Gene_Object_View {
	/**
	 * Redirect to parent upon successful create/modify
	 */
	var $redirectParent = true;
	
	/**
	 * Class Constructor
	 *
	 * @var object $model Gene_Object_Model object
	 */
	function Gene_Object_View_Redirect(&$model) {
		$this->model =& $model;
	}
	
	/**
	 * Show this object. Returns HTML output code for object.
	 *
	 * @var array $actions Additional Arguments
	 *
	 * @return string HTML output for object.
	 */
	function view($actions=array()) {
		$tpl =& Gene::getTemplate();
		
		/* General Object Information */
		$info = $this->model->get();
		
		if (substr($info['content'], 0, 11) == "internal://") {
			header ("Location: ".Gene::makeLink(substr($info['content'], 11)));
		} else {
			header ("Location: ".str_replace (array("\n", "\r"), "", $info['content']));
		}
		
		exit();
	}
	
	/**
	 * Callback function to specify object specific edit actions.
	 */
	function getCustomEditActions() {
		$tpl =& Gene::getTemplate();
		
		/* General Object Information */
		$info = $this->model->get();

		if (substr($info['content'], 0, 11) == "internal://") {
			$tpl->assign ("target", substr($info['content'], 11));
			$tpl->assign ("type", "internal");
		} else {
			$tpl->assign ("target", $info['content']);
			$tpl->assign ("type", "external");
		}
	}
	
	/**
	 * Displays the object creation form for this type of object.
	 *
	 * @static
	 *
	 * @var object $parent Parent Object
	 *
	 * @return string HTML output for object creation.
	 */
	function create(&$parent) {
		$tpl =& Gene::getTemplate();
		return $tpl->fetch("objects/redirect/create");
	}
	
	/**
	 * Creates the new object.
	 * 
	 * Objects can implement their own creation routines here to fiddle
	 * the user input before sending to Gene_Object::create() or return a
	 * PEAR::raiseError if input is invalid
	 */
	function doCreate($type, $title='', $desc='', $content='', &$parentobj, $options=array(), $draft=false) {
		$rtype = Gene_Request::getString("rtype", GENE_REQUEST_POST);
		if ($rtype == "internal") {
			$options = array("type" => "internal");
		} else {
			$options = array("type" => "external");
		}
		return parent::doCreate($type, $title, $desc, $content, $parentobj, $options, $draft);
	}
	
	/**
	 * Edits the current object.
	 *
	 * Objects can implement callback routines here.
	 */
	function doEdit($options=array()) {
		$rtype = Gene_Request::getString("rtype", GENE_REQUEST_POST);
		if ($rtype == "internal") {
			$options['options']['type'] = "internal";
		} else {
			$options['options']['type'] = "external";
		}
		
		return $this->model->setContent($options);
	}
}
 
?>